﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Fixx.Connector
{
    class FixxState
    {
        public Action<FixxStatus> Callback { get; set; }
        public HttpWebRequest Connection { get; set; }

		public bool RaisedCallback { get; set; }

		public string PostData { get; set; }

     	const int BUFFER_SIZE = 1024;
		public StringBuilder RequestData { get; set; }
		public byte[] BufferRead { get; set; }
		public HttpWebResponse Response { get; set; }
		public Stream StreamResponse { get; set; }
		public FixxState()
		{
			BufferRead = new byte[BUFFER_SIZE];
			RequestData = new StringBuilder("");
			Connection = null;
			StreamResponse = null;
		}
		public void RaiseCallBack(Exception e)
		{
			var error = new FixxError()
			{
				Type = FixxErrorType.Uncaught,
				OriginalException = e,
				Message = e.Message
			};

			if (e is WebException)
			{
				error.Type = FixxErrorType.Server;
				var oe = (WebException)e;
				if (oe.Response != null)
				{
					error.HttpStatusCode = ((System.Net.HttpWebResponse)(oe.Response)).StatusCode;
					
				}
				else
				{
					error.Message = "Could not connect to server: " + Connection.Address.ToString();
				}
				
				
			}
			else if (e is Exception)
				error.Type = FixxErrorType.Client;

			if (!this.RaisedCallback)
			{
				this.RaisedCallback = true;
				this.Callback(new FixxStatus(error));
			}
		}
		public void RaiseCallBack()
		{
			if (!this.RaisedCallback)
				this.Callback(new FixxStatus(this.RequestData.ToString()));
		}
    }
}
